package cn.tedu.zhyl.system.service.impl;

import cn.tedu.zhyl.system.mapper.SysDeptMapper;
import cn.tedu.zhyl.system.mapper.SysPostMapper;
import cn.tedu.zhyl.system.pojo.dto.SysPostParams;
import cn.tedu.zhyl.system.pojo.entity.SysPost;
import cn.tedu.zhyl.system.pojo.vo.SysDeptVo;
import cn.tedu.zhyl.system.pojo.vo.SysPostVO;
import cn.tedu.zhyl.system.service.SysPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class SysPostServiceImpl implements SysPostService {
    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<SysPostVO> getList(SysPostParams postParams) {
        QueryWrapper<SysPost> sysPostVOQueryWrapper = new QueryWrapper<SysPost>();
        BeanUtils.copyProperties(postParams,sysPostVOQueryWrapper);
        List<SysPost> sysPosts = sysPostMapper.selectList(sysPostVOQueryWrapper);

        List<SysDeptVo> deptNos=new ArrayList<>();
        for (SysPost sysPost : sysPosts){
            deptNos.add(sysDeptMapper.findByDeptNo(sysPost.getDeptNo()));
        }
        log.debug("deptNos为:{}",deptNos);
        List<SysPostVO> list = new ArrayList<>();
        for (SysPost sysPost : sysPosts) {
            SysPostVO sysPostVO = new SysPostVO();
            BeanUtils.copyProperties(sysPost,sysPostVO);
            list.add(sysPostVO);
        }
        log.debug("实现类中查询部门结果为:{}",list);
        return list;
    }

    @Override
    public void save(SysPostParams params) {
        Long id = params.getId();
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(params,sysPost);
        if(id == null){
            sysPostMapper.insert(sysPost);
        }else {
            UpdateWrapper<SysPost> sysPostUpdateWrapper = new UpdateWrapper<>();
            sysPostUpdateWrapper.eq("id",params.getId());
            sysPostMapper.update(sysPost,sysPostUpdateWrapper);
        }
    }

    @Override
    public void delete(Long id) {
        sysPostMapper.deleteById(id);
    }

}
